//
//  BKZhaoShengViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/2.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKZhaoShengViewController.h"
#import "BKHistoryViewModel.h"
#import "BKZhaoShengView.h"
#import "BKColleageHeaderView.h"
#import "BKHistoryButton.h"
#import "BKResultOfHistoryViewController.h"
#import <MBProgressHUD.h>
#import "BKZSHistoryHeaderView.h"
#import "BKZSHistoryTableViewCell.h"
#import "BKColleageViewModel.h"

@interface BKZhaoShengViewController () <UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet BKColleageHeaderView *headView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) UITableView *historyView;
@property (strong, nonatomic) NSDictionary *historyDict;

@end

@implementation BKZhaoShengViewController
- (IBAction)p_back:(id)sender {
    [_delegate dismiss];
}

- (NSDictionary *)historyDict
{
    if (_historyDict == nil) {
        NSDictionary* provDic =  @{ @"北京":@"1",
                                    @"天津":@"2",
                                    @"上海":@"3",
                                    @"重庆":@"4",
                                    @"河北":@"5",
                                    @"河南":@"6",
                                    @"山东":@"7",
                                    @"山西":@"8",
                                    @"安徽":@"9",
                                    @"江西":@"10",
                                    @"江苏":@"11",
                                    @"浙江":@"12",
                                    @"湖北":@"13",
                                    @"湖南":@"14",
                                    @"广东":@"15",
                                    @"广西":@"16",
                                    @"云南":@"17",
                                    @"贵州":@"18",
                                    @"四川":@"19",
                                    @"陕西":@"20",
                                    @"青海":@"21",
                                    @"宁夏":@"22",
                                    @"黑龙江":@"23",
                                    @"吉林":@"24",
                                    @"辽宁":@"25",
                                    @"西藏":@"26",
                                    @"新疆":@"27",
                                    @"内蒙古":@"28",
                                    @"海南":@"29",
                                    @"福建":@"30",
                                    @"甘肃":@"31"};
        
        [BKColleageViewModel lishifenshuWithCid:_cid kaoquId:provDic[_pro] success:^(NSDictionary *dict) {
            _historyDict = dict;
            [self.historyView reloadData];
        } failure:^(NSString *message) {
            
        }];
        
    }
    return _historyDict;
}

- (IBAction)p_topBtnClick:(id)sender {
    
    __weak BKZhaoShengViewController *weakSelf = self;
    UIButton *button = (UIButton *)sender;
    
    for (id sub in [_headView subviews]) {
        if ([sub isKindOfClass:[UIButton class]]) {
            UIButton *typeButton = (UIButton *)sub;
            if ([typeButton isEqual:button]) {
                typeButton.selected = YES;
            }else{
                typeButton.selected = NO;
            }
        }
    }
    
    if ([button.titleLabel.text isEqualToString:@"往年分数"]) {
        [_contentView bringSubviewToFront:_historyView];
        _historyView.hidden = NO;
    } else {
        [_contentView bringSubviewToFront:_imageView];
        _historyView.hidden = YES;
        
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak BKZhaoShengViewController *weakSelf = self;
    
    [weakSelf loadTableViewGroup];
    
    [self historyDict];
    
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载招生信息";
    [hud show:YES];
    [hud hide:YES];
    
    [self.view layoutSubviews];
    
    _nameLabel.text = _name;
    
    BKHistoryButton *btn = [self.headView.subviews firstObject];
    [self p_topBtnClick:btn];
    
}

- (void)loadTableViewGroup
{
    __weak BKZhaoShengViewController *weakSelf = self;
    UITableView *historyView = [[UITableView alloc] initWithFrame:CGRectMake(8, 0, _contentView.frame.size.width - 8, _contentView.frame.size.height)];
    weakSelf.historyView = historyView;
    [weakSelf.contentView addSubview:weakSelf.historyView];
    weakSelf.historyView.delegate = weakSelf;
    weakSelf.historyView.dataSource = weakSelf;
    weakSelf.historyView.separatorColor = [UIColor clearColor];
    [weakSelf.historyView registerNib:[UINib nibWithNibName:NSStringFromClass([BKZSHistoryTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"ZhaoShenHistory"];
    
    
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [_historyView setFrame:CGRectMake(8, 0, _contentView.frame.size.width - 8, _contentView.frame.size.height)];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // UITableViewCell *cell = nil;
   // if ([tableView isEqual:_historyView]) {
        BKZSHistoryTableViewCell *cell = [_historyView dequeueReusableCellWithIdentifier:@"ZhaoShenHistory"];
    
        NSString *startYear = [NSString stringWithFormat:@"%d",14 - (int)indexPath.row];
        cell.year.text = [NSString stringWithFormat:@"20%@",startYear];
    
        cell.whigh.text = [self getSubStr:self.historyDict[[NSString stringWithFormat:@"w%@h",startYear]]];
        cell.wlow.text = [self getSubStr:self.historyDict[[NSString stringWithFormat:@"w%@l",startYear]]];
        cell.waver.text = [self getSubStr:self.historyDict[[NSString stringWithFormat:@"w%@a",startYear]]];
        cell.lhigh.text = [self getSubStr:self.historyDict[[NSString stringWithFormat:@"l%@h",startYear]]];
        cell.llow.text = [self getSubStr:self.historyDict[[NSString stringWithFormat:@"l%@l",startYear]]];
        cell.laver.text = [self getSubStr:self.historyDict[[NSString stringWithFormat:@"l%@a",startYear]]];
    
    
    if (indexPath.row % 2 == 0) {
        cell.backgroundColor = [UIColor clearColor];
    }else{
        cell.backgroundColor = [UIColor colorWithRed:0.914 green:0.965 blue:0.980 alpha:1.000];
    }
//return cell;
  //  }
    return cell;
}

- (NSString *)getSubStr:(NSString *)str
{
    return str.length > 3 ? [str substringToIndex:3]:str;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([tableView isEqual:_historyView]) {
        return  3;
    }
    
    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[NSBundle mainBundle] loadNibNamed:@"BKZSHistoryHeaderView" owner:self options:nil][0];
    return view;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 34;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}


@end
